<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>传智健康</title>
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
    <!-- 引入组件库 -->
    <script type="text/javascript" src="../js/jquery.min.js"></script>
    <script src="../js/vue.js"></script>
    <script src="../plugins/elementui/index.js"></script>
    <script src="../js/axios-0.18.0.js"></script>
    <style>
        .avatar-uploader .el-upload {
            border: 1px dashed #d9d9d9;
            border-radius: 6px;
            cursor: pointer;
            position: relative;
            overflow: hidden;
        }

        .avatar-uploader .el-upload:hover {
            border-color: #409EFF;
        }


        .datatable td, .datatable th {
            padding: 12px 0;
            min-width: 0;
            -webkit-box-sizing: border-box;
            box-sizing: border-box;
            text-overflow: ellipsis;
            vertical-align: middle;
            position: relative;
            text-align: left;
        }
    </style>
</head>
<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>系统设置<small>权限管理</small></h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>系统设置</el-breadcrumb-item>
            <el-breadcrumb-item>权限管理</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">
            <div class="filter-container">
                <el-input placeholder="id/名称/关键字" v-model="pagination.queryString" style="width: 200px;"
                          class="filter-item" @keyup.enter.native="handleFilter"></el-input>
                <el-button @click="findPage()" class="dalfBut">查询</el-button>
                <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
            </div>
            <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                <el-table-column type="index" align="center" label="序号"></el-table-column>
                <el-table-column prop="name" label="权限名称" align="center"></el-table-column>

                <el-table-column prop="keyword" label="权限关键字" align="center"></el-table-column>
                <el-table-column prop="description" label="详细描述" align="center"></el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                        <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination-container">
                <el-pagination
                        class="pagiantion"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        layout="total, prev, pager, next, jumper"
                        :total="pagination.total">
                </el-pagination>
            </div>
            <!-- 新增标签弹层 -->
            <div class="add-form">
                <el-dialog title="新增权限" :visible.sync="dialogFormVisible">
                    <el-form :model="formData" :rules="rules" ref="ruleForm" label-position="right"
                             label-width="100px">
                        <el-row>
                            <el-col :span="18">
                                <el-form-item label="权限名称" prop="name">
                                    <el-input v-model="formData.name" @blur="verification_name()"/>
                                </el-form-item>
                            </el-col>
                        </el-row>

                        <el-row>

                            <el-col :span="18">
                                <el-form-item label="关键字" prop="keyword">
                                    <el-input v-model="formData.keyword"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="详细描述">
                                    <el-input v-model="formData.description" type="textarea"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>

                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="handleAdd()">确定</el-button>
                    </div>
                </el-dialog>
            </div>


            <!-- 编辑标签弹层 -->
            <div class="add-form1">
                <el-dialog title="编辑权限" :visible.sync="dialogFormVisible4Edit">
                    <el-form :model="formData" ref="ruleForm1" :rules="rules" label-position="right"
                             label-width="100px">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="权限id" prop="id">
                                    <el-input v-model="formData.id" readonly/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="18">
                                <el-form-item label="权限名称" prop="name">
                                    <el-input v-model="formData.name"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="18">
                                <el-form-item label="权限关键字" prop="keyword">
                                    <el-input v-model="formData.keyword"/>
                                </el-form-item>
                            </el-col>
                        </el-row>

                        <el-col :span="24">
                            <el-form-item label="详细描述">
                                <el-input v-model="formData.description" type="textarea"></el-input>
                            </el-form-item>
                        </el-col>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                        <el-button type="primary" @click="handleEdit()">确定</el-button>
                    </div>
                </el-dialog>
            </div>

        </div>
    </div>
</div>
</body>

<script>
    var verification_flag;
    var checkName = (rule, value, callback) => {
        if (!value) {
            return callback(new Error('权限名称为必填项'));
        }
        setTimeout(() => {
            if (!verification_flag) {
                callback(new Error('权限名称重复'));
            } else {
                callback();
            }
        }, 500);
    }

    var vue = new Vue({

        el: '#app',
        data: {

            readonly: true,//只读
            pagination: {//分页相关属性
                currentPage: 1,
                pageSize: 10,
                total: 100,
                queryString: null,
            },
            dataList: [],//列表数据
            formData: {},//表单数据
            dialogFormVisible: false,//控制添加窗口显示/隐藏
            dialogFormVisible4Edit: false,//控制编辑窗口显示/隐藏
            rules: {//校验规则
                keyword: [{required: true, message: '权限关键字为必填项', trigger: 'blur'}],
                // name: [{required: true, message: '权限名称为必填项', trigger: 'blur'}],
                name: [{validator: checkName, trigger: 'blur'}]
            },
        },
        created() {
            this.findPage();
        },
        methods: {
            //添加
            handleAdd() {
                this.$refs['ruleForm'].validate((valid) => {
                    if (valid) {
                        axios.post("/permission/add.do", this.formData).then(res => {
                            this.resetForm();
                            //弹出窗口
                            this.dialogFormVisible = false;
                            this.$message({
                                message: res.data.message,
                                type: res.data.flag ? 'success' : 'error'
                            });
                            this.findPage();//分页查询
                        })

                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                })
            },

            //分页查询
            findPage() {
                var parms = {
                    currentPage: this.pagination.currentPage,
                    pageSize: this.pagination.pageSize,
                    queryString: this.pagination.queryString
                }

                axios.post("/permission/findPage.do", parms).then(res => {
                    //后台返回的是PageResult total rows
                    this.pagination.total = res.data.total;
                    this.dataList = res.data.rows;
                })
            },
            //编辑
            handleEdit() {
                this.$refs['ruleForm1'].validate((valid) => {
                    if (valid) {
                        axios.post("/permission/edit.do", this.formData).then(res => {
                            this.dialogFormVisible4Edit = false;
                            this.$message({
                                message: res.data.message,
                                type: res.data.flag ? 'success' : 'error'
                            });
                            this.findPage();//分页查询
                        })
                    }else {
                        console.log('error submit!!');
                        return false;
                    }
                })
            },
            handleUpdate(row) {
                this.$nextTick(() => {
                    this.$refs['ruleForm1'].clearValidate();
                })
                //1.	弹出编辑窗口
                this.dialogFormVisible4Edit = true;
                this.resetForm();
                //回显
                axios.get("/permission/findById.do?permissionId=" + row.id).then(res => {
                    this.formData = res.data.data;//回显
                    this.$message({
                        message: res.data.message,
                        type: 'success'
                    });

                })
            },

            handleDelete(row) {

                const h = this.$createElement;
                //发送查询占用权限用户请求
                axios.get("/permission/findCountByPermissionId.do?id=" + row.id).then(res => {
                    const role = [];
                    if (res.data.flag) {
                        const ok = '未被使用，是否删除？';
                        role.push(h('p', null, ok))
                    } else {
                        const pre = '此权限已被 ';
                        const post = ' 使用，是否要强制删除？';
                        const Datamessage = res.data.data;
                        role.push(h('p', null, pre))
                        for (const i in Datamessage) {
                            role.push(h('p', null, Datamessage[i].name))
                        }
                        role.push(h('p', null, post))
                    }
                    console.log(res.data.data),
                        this.$confirm('提示', {
                            message: h('div', null, role),
                            confirmButtonText: '确定',
                            cancelButtonText: '取消',
                            type: 'warning'
                        }).then(() => {
                            //     //发送查询占用权限用户请求
                            axios.get("/permission/deleteById.do?id=" + row.id).then(res => {
                                this.$message({
                                    message: res.data.message,
                                    type: res.data.flag ? 'success' : 'error'
                                });
                                this.findPage();
                            })
                        }).catch(() => {
                            this.$message({
                                type: 'info',
                                message: '已取消删除'
                            });
                        });
                })
            },


            // 重置表单
            resetForm() {
                this.formData = {};
            },
            // 弹出添加窗口
            handleCreate() {
                this.$nextTick(() => {
                    this.$refs['ruleForm'].clearValidate();
                })
                this.resetForm();
                this.dialogFormVisible = true;
                //1.重置表单
            },
            //切换页码
            handleCurrentChange(currentPage) {
                this.pagination.currentPage = currentPage;
                this.findPage();
            },

            //验证重复名字
            verification_name() {
                var param = {
                    p: this.formData.name
                };
                axios.post("/permission/verification_name.do", param).then(res => {
                    verification_flag = res.data.flag;
                })
            }
        }
    })

</script>
</html>
